package br.com.massai.model.dao;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import br.com.massai.controller.filter.SessionControl;
import br.com.massai.model.entity.Obra;
import br.com.massai.model.entity.PlanilhaLancamento;
import br.com.massai.model.entity.Usuario;
import br.com.massai.model.jdbc.OracleJDBC;
import br.com.massai.model.repository.ObraRepository;
import br.com.massai.view.enums.StatusObraEnum;
import br.com.massai.view.utils.FacesUtil;

@SuppressWarnings({ "unchecked" })
public class ObraDAO extends GenericDAO<Obra> implements ObraRepository {

	private static final long serialVersionUID = 12787544565863938L;

	@Inject private OracleJDBC oracleJDBC;
	@Inject private SessionControl session;

	@Override
	public List<Obra> obterObrasGrupoSelecionada() throws Exception {
		try{
			List<Obra> obrasGeral = new ArrayList<Obra>();
			obrasGeral = em.createQuery("SELECT o FROM Obra o WHERE o.status = 'T'").getResultList();
			return obrasGeral;

		}catch (Exception e) {
			throw new Exception(e);
		}
	}

	@Override
	public List<Obra> obterObrasPermitidas(PlanilhaLancamento planilha) throws Exception {
		try{
			if(!session.ehAdministrador()){
				StringBuilder query = new StringBuilder("SELECT o FROM Obra o JOIN o.listaUsuarios users WHERE :user IN (users)");

				if(planilha.getId() == null){
					query.append(" AND o.status = :status");
					return em.createQuery(query.toString()).setParameter("user", session.getUsuario()).setParameter("status", StatusObraEnum.T).getResultList();
				}

				return em.createQuery(query.toString()).setParameter("user", session.getUsuario()).getResultList();
			}
			
			StringBuilder query = new StringBuilder("SELECT o FROM Obra o");

			if(planilha.getId() == null){
				query.append(" WHERE o.status = :status");
				return em.createQuery(query.toString()).setParameter("status", StatusObraEnum.T).getResultList();
			}
			return em.createQuery(query.toString()).getResultList();

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	@Override
	public List<Obra> obterObrasComGrupo() throws Exception {
		try{
			List<Obra> obrasComGrupo = em.createQuery("SELECT o FROM Obra o WHERE o.grupoProducao <> null").getResultList();

			if(obrasComGrupo != null && !obrasComGrupo.isEmpty())
				for(Obra oc : obrasComGrupo)
					oc.setSelect(true);
			return obrasComGrupo;

		}catch (Exception e) {
			throw new Exception(e);
		}
	}

	@Override
	public Obra obterPorCodigo(Long id) throws Exception {
		return oracleJDBC.obterObrasPorCodigo(id);
	}

	public ObraDAO() {
		super(Obra.class);
	}
	
	@Override
	public void salvarObra(Obra o) throws Exception {
		if(o.getId() == null)
			super.criar(o);
		else
			super.atualizar(o);
	}
	
	@Override
	public Obra obterObraDeptoColigada(String codDepto, Long coligada) throws Exception {
		return oracleJDBC.obterObraDeptoColigada(codDepto, coligada);
	}
	
	@Override
	public List<Obra> obterObrasSemGrupo() throws Exception {
		return oracleJDBC.obterObrasAtivas();
	}

	@Override
	public List<Obra> obterObraPorResponsavel() throws Exception {
		return null;
	}

	@Override
	public List<Obra> obterObraPorResponsavel(Usuario user) throws Exception {
		return null;
	}

	@Override
	public List<Obra> obterObjetoObra(List<Long> ids) throws Exception {
		if(ids != null && !ids.isEmpty())
			return oracleJDBC.obterObjetoObra(ids);
		return null;
	}
	
	@Override
	public List<Obra> obterPorExemplo(Obra o) throws Exception {
		if(o == null) return oracleJDBC.obterPorExemplo(null);

		String clauseWhere = new String();
		if(o.getNomeFantasia() != null && !o.getNomeFantasia().isEmpty())
			clauseWhere += " and lower(nomefantasia) like lower('%"+ o.getNomeFantasia() +"%') ";

		return oracleJDBC.obterPorExemplo(clauseWhere);
	}
	
	@Override
	public List<Obra> obterObrasAssociadas() throws Exception {
		try{
			return super.obterTodos();
		} catch (Exception e) {
			FacesUtil.registrarMensagemErro(e.getMessage());
			return null;
		}
	}
}